#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import locale
import http.server
import socketserver
import urllib
import urllib.request
import cgi
import platform

REAL_EVENT_DETAIL_URL = "http://www.mysee.cc:18308/mse-web/mysee/search/searchRescueById.do";
def log(str):
    print(str);

def getEventDetail(requrl, test_data) :
    test_data_urlencode = test_data.encode("utf-8");
    if (requrl != None and not requrl.startswith("http://")):
        requrl = "http://www.mysee.cc:18308" + requrl;
    req = urllib.request.Request(url = requrl,data = test_data_urlencode)
    req.add_header("Content-Type", "application/json");
    try:
        res_data = urllib.request.urlopen(req, timeout=15)
        res = res_data.read()
        return res;
    except:
        pass;
    return b"{}";

def parse():
    global REAL_EVENT_DETAIL_URL
    url = REAL_EVENT_DETAIL_URL
    query = os.environ["QUERY_STRING"]
    url = url + "?" +  query;
    form = cgi.FieldStorage();
    eventId = form.getvalue("id");
    data = '{"id":"%s"}' % eventId; 
    response = getEventDetail(url, data);
    tmp = "callback(" + response.decode("utf-8") + ")";
    log(tmp);
if platform.system().lower() == "windows":
    print('Content-type: text/html;charset=gbk')
else:
    print('Content-type: text/html;charset=utf-8')
print();
try:
    parse();
except:
    #出现异常的情况下返回空数据
    print("callback({})")
